var t = require;
var e = module;
cc.Class({
    extends: cc.Component,
    properties: {
        layoutRoot: cc.Node,
        layoutLeft: cc.Layout,
        layoutRight: cc.Layout,
        prefabLcWord: cc.Prefab,
        g: cc.Graphics
    },
    onDestroy: function onDestroy() {
        this.winTimeout && clearTimeout(this.winTimeout);
    },
    start: function start() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this),
            this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this),
            this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this),
            this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this),
            (this.level = lGlobal.curLevel),
            (this.lcWordTotal = []),
            (this.lcWordLeft = []),
            (this.lcWordRight = []);
        for (
            var t = lGlobal.lvCfg.json[lGlobal.curModel][lGlobal.curLevel], e = [], o = [], i = 0;
            i < t.uris.length;
            i++
        ) {
            (e[i] = i), (o[i] = i);
        }
        for (var n = 0; n < t.uris.length; n++) {
            var s = Math.floor(Math.random() * e.length),
                h = e[s];
            e.splice(s, 1);
            var l = Math.floor(Math.random() * o.length),
                c = o[l];
            o.splice(l, 1);
            var d = t.typeLeft,
                r = t.typeRight;
            t.uris[n].typeLeft && (d = t.uris[n].typeLeft), t.uris[n].typeRight && (r = t.uris[n].typeRight);
            var a = t.uris[n].uri,
                g = cc.instantiate(this.prefabLcWord),
                u = g.getComponent("LcWord");
            u.setType(d, a, "left", this.lcWordTouchCb.bind(this)), (g.parent = this.layoutLeft.node), (g.zIndex = h);
            var f = cc.instantiate(this.prefabLcWord),
                S = f.getComponent("LcWord");
            S.setType(r, a, "right", this.lcWordTouchCb.bind(this)),
                (f.parent = this.layoutRight.node),
                (f.zIndex = c),
                this.lcWordLeft.push(u),
                this.lcWordRight.push(S),
                this.lcWordTotal.push(u),
                this.lcWordTotal.push(S);
        }
        (this.nowLeftSelected = null),
            (this.nowRightSelected = null),
            (this.startDragged = null),
            (this.endDragged = null),
            (this.inDrag = !1);
    },
    onTouchStart: function onTouchStart(t) {
        for (var e = t.getLocation(), o = 0; o < this.lcWordTotal.length; o++) {
            var i = this.lcWordTotal[o].node;
            this.lcWordTotal[o] && this.lcWordTotal[o].sound && this.lcWordTotal[o].sound.stop(),
                i.getBoundingBoxToWorld().contains(e) && this.lcWordTotal[o].onTouchStart(t);
        }
    },
    onTouchMove: function onTouchMove(t) {
        this.inDrag = !0;
        for (var e = t.getLocation(), o = !1, i = 0; i < this.lcWordTotal.length; i++) {
            this.lcWordTotal[i].node.getBoundingBoxToWorld().contains(e) &&
                ((o = !0), this.lcWordTotal[i].onTouchMove(t));
        }
        o || (this.endDragged && (this.endDragged.unselected(), (this.endDragged = null))),
            (this.touchMoveLocation = e);
    },
    onTouchEnd: function onTouchEnd(t) {
        (this.inDrag = !1), (this.touchMoveLocation = null);
        for (var e = t.getLocation(), o = 0; o < this.lcWordTotal.length; o++) {
            this.lcWordTotal[o].node.getBoundingBoxToWorld().contains(e) && this.lcWordTotal[o].onTouchEnd(t);
        }
        if (this.startDragged) {
            if (this.endDragged)
                if ((console.log("连线对比"), this.startDragged.cname == this.endDragged.cname)) {
                    for (var i = 0; i < this.lcWordTotal.length; i++) {
                        this.lcWordTotal[i] && this.lcWordTotal[i].sound && this.lcWordTotal[i].sound.stop();
                    }
                    this.startDragged.lined(this.endDragged),
                        this.endDragged.lined(this.startDragged),
                        this.rightLine(this.startDragged, this.endDragged),
                        this.checkWin();
                } else this.wrongLine(this.startDragged, this.endDragged);
        } else
            this.nowLeftSelected &&
                this.nowRightSelected &&
                (console.log("点选对比"),
                this.nowLeftSelected.cname == this.nowRightSelected.cname
                    ? (this.nowLeftSelected.lined(this.nowRightSelected),
                      this.nowRightSelected.lined(this.nowLeftSelected),
                      this.rightLine(this.nowLeftSelected, this.nowLeftSelected),
                      this.checkWin())
                    : this.wrongLine(this.nowLeftSelected, this.nowRightSelected),
                this.startDragged && (this.startDragged.unselected(), (this.startDragged = null)),
                this.endDragged && (this.endDragged.unselected(), (this.endDragged = null)),
                this.nowLeftSelected && (this.nowLeftSelected.unselected(), (this.nowLeftSelected = null)),
                this.nowRightSelected && (this.nowRightSelected.unselected(), (this.nowRightSelected = null)));
        this.startDragged && (this.startDragged.unselected(), (this.startDragged = null)),
            this.endDragged && (this.endDragged.unselected(), (this.endDragged = null));
    },
    onTouchCancel: function onTouchCancel() {},
    lcWordTouchCb: function lcWordTouchCb(t, e) {
        if ("onTouchStart" == t) {
            if (e.isSelected)
                e.unselected(),
                    this.nowLeftSelected == e && (this.nowLeftSelected = null),
                    this.nowRightSelected == e && (this.nowRightSelected = null);
            else {
                if ("left" == e.leftOrRight) {
                    for (var o = 0; o < this.lcWordLeft.length; o++) {
                        this.lcWordLeft[o].unselected();
                    }
                    (this.nowLeftSelected = e), (this.startDragged = this.nowLeftSelected);
                } else if ("right" == e.leftOrRight) {
                    for (var i = 0; i < this.lcWordRight.length; i++) {
                        this.lcWordRight[i].unselected();
                    }
                    (this.nowRightSelected = e), (this.startDragged = this.nowRightSelected);
                }
                e.selected(),
                    this.nowLeftSelected &&
                        this.nowRightSelected &&
                        (this.nowLeftSelected.cname == this.nowRightSelected.cname
                            ? (this.nowLeftSelected.lined(this.nowRightSelected),
                              this.nowRightSelected.lined(this.nowLeftSelected),
                              this.rightLine(this.nowLeftSelected, this.nowRightSelected),
                              this.checkWin())
                            : this.wrongLine(this.nowLeftSelected, this.nowRightSelected),
                        this.startDragged && (this.startDragged.unselected(), (this.startDragged = null)),
                        this.endDragged && (this.endDragged.unselected(), (this.endDragged = null)),
                        this.nowLeftSelected && (this.nowLeftSelected.unselected(), (this.nowLeftSelected = null)),
                        this.nowRightSelected && (this.nowRightSelected.unselected(), (this.nowRightSelected = null)));
            }
        } else
            "onTouchMove" == t
                ? this.startDragged
                    ? this.startDragged.leftOrRight != e.leftOrRight &&
                      (this.endDragged
                          ? this.endDragged != e &&
                            (this.endDragged.unselected(),
                            (this.endDragged = null),
                            (this.endDragged = e),
                            e.selected())
                          : ((this.endDragged = e), e.selected()))
                    : ((this.startDragged = e), this.startDragged.selected())
                : "onTouchEnd" == t &&
                  (e == this.startDragged
                      ? (this.startDragged = null)
                      : this.endDragged ||
                        (this.startDragged &&
                            this.startDragged.leftOrRight != e.leftOrRight &&
                            ((this.endDragged = e), e.selected())));
    },
    wrongLine: function wrongLine(t, e) {
        console.log("错误动画"),
            this.nowLeftSelected && (this.nowLeftSelected.unselected(), (this.nowLeftSelected = null)),
            this.nowRightSelected && (this.nowRightSelected.unselected(), (this.nowRightSelected = null)),
            soundMgr.playSound(soundMgr.wrong),
            cc
                .tween(t.node)
                .to(0.05, {angle: -5})
                .to(0.1, {angle: 5})
                .to(0.1, {angle: -5})
                .to(0.1, {angle: 5})
                .to(0.05, {angle: -0})
                .start(),
            cc
                .tween(e.node)
                .to(0.05, {angle: -5})
                .to(0.1, {angle: 5})
                .to(0.1, {angle: -5})
                .to(0.1, {angle: 5})
                .to(0.05, {angle: -0})
                .start();
    },
    rightLine: function rightLine(t, e) {
        console.log("正确动画"),
            this.nowLeftSelected && (this.nowLeftSelected.unselected(), (this.nowLeftSelected = null)),
            this.nowRightSelected && (this.nowRightSelected.unselected(), (this.nowRightSelected = null)),
            soundMgr.playSound(soundMgr.right),
            cc.tween(t.node).to(0.33, {scale: 1.2}).delay(0.33).to(0.33, {scale: 1}).start(),
            cc.tween(e.node).to(0.33, {scale: 1.2}).delay(0.33).to(0.33, {scale: 1}).start();
    },
    checkWin: function checkWin() {
        var t = this;
        console.log("胜利判断");
        for (var e = !0, o = 0; o < this.lcWordLeft.length; o++) {
            if (!this.lcWordLeft[o].slined) {
                e = !1;
                break;
            }
        }
        e &&
            (this.winTimeout = setTimeout(function () {
                window.ui.win({model: "LineConnect", lv: t.level});
            }, 1500));
    },
    update: function update() {
        this.g.clear();
        for (var t = 0; t < this.lcWordLeft.length; t++) {
            var e = this.lcWordLeft[t];
            if (e.slined) {
                var o = e.node.convertToWorldSpaceAR(e.nodeLineStart.position),
                    i = this.g.node.convertToNodeSpaceAR(o);
                this.g.moveTo(i.x, i.y);
                var n = e.slined.node.convertToWorldSpaceAR(e.slined.nodeLineStart.position),
                    s = this.g.node.convertToNodeSpaceAR(n);
                (this.g.strokeColor = e.mask.color), this.g.lineTo(s.x, s.y), this.g.stroke();
            }
        }
        if (this.inDrag && this.startDragged && this.touchMoveLocation) {
            var h = this.startDragged.node.convertToWorldSpaceAR(this.startDragged.nodeLineStart.position),
                l = this.g.node.convertToNodeSpaceAR(h);
            this.g.moveTo(l.x, l.y);
            var c = this.g.node.convertToNodeSpaceAR(this.touchMoveLocation);
            (this.g.strokeColor = new cc.Color().fromHEX("#FFF000")), this.g.lineTo(c.x, c.y), this.g.stroke();
        }
    }
});
